package com.example.springsecurity.security.rbac;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * 核心就是判断当前的用户所拥有的URL是否和当前访问的URL是否匹配。
 * @Author: xieyonghui
 * @Date: 2019/7/10
 * @Description:
 */
@Component("rbacService")
public class RbacServiceImpl implements RbacService{

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
        Object principal = authentication.getPrincipal();
        boolean hasPermission = false;

        //首先判断先当前用户是否是我们UserDetails对象。
        if (principal instanceof UserDetails) {
            String userName = ((UserDetails) principal).getUsername();

            // 数据库读取 //读取用户所拥有权限的所有URL
            Set<String> urls = new HashSet<>();

            urls.add("/who");
            // 注意这里不能用equal来判断，因为有些URL是有参数的，所以要用AntPathMatcher来比较
            for (String url : urls) {
                if (antPathMatcher.match(url, request.getRequestURI())) {
                    hasPermission = true;
                    break;
                }
            }
        }
        return hasPermission;
    }
}
